/*****************************************************************************

 Description: Midlet creating a canvas that does load a svg image file 
                which is rendered in the paint method.
                    
 Created By: Oscar Vivall 2007-01-11
 @file        SVGStatic.java

 COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004.
 The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
 The use of the software is subject to the terms of the end-user license 
 agreement which accompanies or is included with the software. The software is 
 provided "as is" and Sony Ericsson specifically disclaim any warranty or 
 condition whatsoever regarding merchantability or fitness for a specific 
 purpose, title or non-infringement. No warranty of any kind is made in 
 relation to the condition, suitability, availability, accuracy, reliability, 
 merchantability and/or non-infringement of the software provided herein.

 *****************************************************************************/

import java.io.InputStream;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableGraphics;

import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGSVGElement;

public class SVGStatic extends javax.microedition.midlet.MIDlet implements CommandListener{
    private SVGStaticCanvas canvas;
    private Display display;
    private Command cmInfo;
    private Command cmExit;
    private Command cmVoltar;
    private Vector linha, coluna;
//    private int[] largura;
    private int largura;
    private CustomItem tabela;
    private Form form;
    
    public SVGStatic(){
        display = Display.getDisplay(this);
        cmInfo = new Command("Info", Command.ITEM, 2);
        cmExit = new Command("Sair", Command.EXIT, 1);
        cmVoltar = new Command("Voltar", Command.BACK, 3);
//        linha = new Vector();
        coluna = new Vector();    
        coluna.addElement("ID");
        coluna.addElement("NOME");
        coluna.addElement("REGIAO");
        coluna.addElement("POPULACAO");
//        largura = new int[2];
//        largura[0] = 20;
//        largura[1] = 220;
//        largura = 370;
        
        canvas = new SVGStaticCanvas();
        largura = canvas.getWidth();
                
        //tabela = new JMTable(coluna, linha, 1, largura);
    }

    public void startApp() {
    	canvas.addCommand(cmInfo);
    	canvas.addCommand(cmExit);
    	canvas.setCommandListener(this);
        display.setCurrent(canvas);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    	display = null;
    	canvas = null;
    	form = null;
    }
    public class SVGStaticCanvas extends Canvas implements Runnable{
        public SVGImage svgImage = null;
        private ScalableGraphics gc;

        public SVGStaticCanvas(){
            gc = ScalableGraphics.createInstance();

            try{
                InputStream is = getClass().getResourceAsStream("/wms2.svg");
                svgImage = (SVGImage) SVGImage.createImage(is, null);
                
                //Document doc = svgImage.getDocument();
                
                /*SVGElement text = (SVGElement)doc.getElementById("id.1");
                SVGSVGElement text2 = (SVGSVGElement)doc.getElementById("states.3");
                linha = new Vector();
                String[] dados = {"id.1", "Delaware", "S Atl", "102776.0"}; 
                linha.addElement(dados);
                String texto = text.getTrait("#text");
                String text1 = texto.substring(0, texto.indexOf(" "));
                System.out.println("Texto1: " + text1);
                System.out.println("REGIAO: " + text.getTrait("#text"));
                linha.addElement((String)text.getTrait("#text"));
                
                System.out.println("REGIAO: " + text2.getTrait("#regiao"));*/
             
            }catch(Exception e){
                e.printStackTrace();
            }

            new Thread(this).start();
        }

        public void paint(Graphics g){
            g.setColor(0xFFFFFF);
            g.fillRect(0, 0, getWidth(), getHeight());
            
            gc.bindTarget(g);
            svgImage.setViewportWidth(getWidth());
            svgImage.setViewportHeight(getHeight());
            gc.render(0, 0, svgImage);
            gc.releaseTarget();
 
        }
            
		public void run() {
            while(true){
                repaint();
                try{
                    Thread.sleep(50);
                }catch(Exception e){}
            }
        }
    }
        
        public void commandAction(Command c, Displayable s){
        	if(c == cmExit){
        		try{
        			destroyApp(false);
        			notifyDestroyed();
        		}catch(Exception e){e.printStackTrace();}
        	}
        	if(c == cmInfo){
        		form = new Form("Atributos");
//        		form.append((String)linha.firstElement());
        		//form.append(tabela);
        		form.addCommand(cmExit);
        		form.addCommand(cmVoltar);
        		form.setCommandListener(this);
        		display.setCurrent(form);
        		
        	}
        	if(c == cmVoltar){
        		display.setCurrent(canvas);
        	}
        }
}  
